<?php

namespace App\Http\Controllers\Home;
use Hash;
use DB;
use Illuminate\Http\Request;
use App\Http\Requests\UserInsertRequest;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Gregwar\Captcha\CaptchaBuilder;

class WenController extends Controller
{     
     //文章列表页
     public function getWenzhang()
     {     
           //查数据库里的文章
           $data = DB::table('article_list')->get();

           //查询数据库里的用户数量
           $res = DB::table('shop_users')->get();
           $count = count($res);
           // dd($count);
           //解析模板
           return view('Home.user.lie',["data"=>$data,"count"=>$count]);

     }


     //文章详情页
     public function getWen(Request $request)
     {     
            //判断用户是否登录
           if(empty(session('uid'))){
           return  redirect('Home/login')->with('error','请您登陆');
           }  

           $aid = $request->input('aid');

           //获取数据库的数据
           $data = DB::table('article_list')->where('aid',$aid)->first();
           
           //获取用户名称
           $res = DB::table('shop_users')->where('uid',$data->uid)->first();
           $user = $res->uname;

           //获取评论
           $res = DB::table('pinglun')->where('aid',$aid)->get();
        
           // //获取回复
           // if(!empty(session('pid'))){

              
           //    $date = DB::table('huifu')->where('pid',session('pid'))->get();
           // }
          

           //获取登陆者的信息
           $users = DB::table('shop_users')->where('uid',session('uid'))->first();
          
           //解析模板
           return view('Home.user.wen',["data"=>$data,"user"=>$user,"users"=>$users,"res"=>$res]);

     }

    //发表文章页面
      public function getFa()
     {
           //解析模板
           return view('Home.user.fa');

     }

     //接受发表文章内容
     public function postNai(Request $request)
     {
           //判断用户是否登录
           if(empty(session('uid'))){
           return  redirect('Home/login')->with('error','请您登陆');
           }  
           
           //获取照片
           $rea = $request->only(['pic']);

           //获取发表文章的内容
           $data = $request->only(['atitle','aomnt']);
           

           //判断
           if(empty($data['atitle']) || empty($data['aomnt'])){
                return back();
           }

           //判断是否上传照片
           if(!empty($rea['pic'])){

              //调用函数处理头像上传
              $data['pic'] = $this->upload($request,'pic');
            
           }

           //获取发表时间
           $time = time();
           $data['atime'] = $time;

           //获取发表者的uid
           $data['uid'] = session('uid');

           //获取发送者的昵称
           $res = DB::table('shop_users')->where('uid',session('uid'))->select('nicheng')->first(); 

           $data['nicheng'] = $res->nicheng;

           //插入数据库
           $res = DB::table('article_list')->where('uid',session('uid'))->insert($data);
           if($res){
         
                    return redirect('/Wen/wenzhang');
           }else{
        
                    return back()->with('error','发表失败');
           }
     }

        //封装方法 进行头像上传
      private function upload($request,$filename)
      {
           if($request->hasFile($filename)){
              //获取文件后缀名
              $suffix = $request->file($filename)->getClientOriginalExtension();
              // dd($suffix);
              //随机文件名
              $name = md5(time().rand(1,9999));
              $request->file($filename)->move('./uploads',$name.'.'.$suffix);
              return '/uploads/'.$name.'.'.$suffix;
           }
      }
      

      //处理文章评论
      public function postPing(Request $request)
      {

            //获取传过来的值
            $data = $request->only(['aid','ping']);

            //判断
            if(empty($data['ping'])){
              return back();
            }

            //插入数据库
            $res = DB::table('pinglun')->where('aid',$data['aid'])->insert($data);
            if($res){
                 return back();
            }
      }
      

      //处理评论回复
      // public function postHui(Request $request)
      // {
      //       //获取传过来的值
      //       $data = $request->only(['pid','hui']);

      //       //判断
      //       if(empty($data['hui'])){
      //         return back();
      //       }

      //       //把回复某条评论的pid存入session中
      //       $request->session()->put('pid',$data['pid']);

      //       //将回复语言插入数据库
      //       $res = DB::table('huifu')->where('pid',$data['pid'])->insert($data);
      //       if($res){
      //             return back();
      //       }
      // }
}